﻿using Microsoft.Phone.Tasks;
using Microsoft.Xna.Framework.Media;
using Smile.Imaging;
using Smile.Compatibility;
using System;
using System.IO;
using System.Threading.Tasks;
using Smile.Network.Http;

namespace Smile.Mvvm.Service
{
    /// <summary>Service which manage operations with media storage.</summary>
    public class MediaStorageService : IMediaStorageService
    {
        // @Properties
        public object CameraCapture
        {
            get 
            { 
                throw new NotImplementedException();
            }
        }

        // @Public
        public async Task<IImage> CameraCaptureAsync()
        {
            CameraCaptureTask cameraCaptureTask = new CameraCaptureTask();
            var result = await cameraCaptureTask.ShowAsync();

            // Capture succesful
            if (result.TaskResult == TaskResult.OK)
            {
                return new ImageJpeg(result.ChosenPhoto, "ImageCaptured");
            }

            // Capture failed
            else if (result.TaskResult == TaskResult.None)
            {
                throw new Exception("MediaStorageService: CameraCapture() failed");
            }

            // Capture canceled
            else
            {
                return null;
            }
        }

        public async Task<IImage> PhotoChooseAsync()
        {
            PhotoChooserTask photoChooserTask = new PhotoChooserTask();
            var result = await photoChooserTask.ShowAsync();

            // Choosing succesful
            if (result.TaskResult == TaskResult.OK)
            {
                return new ImageJpeg(result.ChosenPhoto, "ImageCaptured");
            }

            // Choosing failed
            else if (result.TaskResult == TaskResult.None)
            {
                throw new Exception("MediaStorageService: CameraCapture() failed");
            }

            // Choosing canceled
            else
            {
                return null;
            }
        }

        public async Task<bool> SavePictureAsync(string title, byte[] byteArray)
        {
            return await Task<bool>.Run(() =>
                {
                    try
                    {
                        _library.SavePicture(title, byteArray);
                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                });
        }

        public async Task<bool> SavePictureAsync(string title, string uriString)
        {
            return await SavePictureAsync(title, new Uri(uriString));
        }

        public async Task<bool> SavePictureAsync(string title, Uri uri)
        {
            var request = new HttpRequest();
            HttpResponse response = await request.GetAsync(uri);

            if (response.Status != System.Net.HttpStatusCode.OK)
            {
                return false;
            }
            return await SavePictureAsync(title, response.ContentBytes);
        }


        // @Private
        MediaLibrary _library = new MediaLibrary();
    }
}
